﻿using Awsc.DataAcquisitionSys.Core.Model;
using System.Text;


namespace Awsc.DataAcquisitionSys.Core.Models.ServiceModels
{
    public class AlarmInformation
    {
        public string AlarmId { get; set; }
        public AlarmLevel AlarmLevel { get; set; }
        public string Description { get; set; }
        public string RuleName { get; set; }
        public DateTime OccurredAt { get; set; }
        public Dictionary<string, object> SensorDatas = new Dictionary<string, object>();
        
        public string GetSensorDataString()
        {
            StringBuilder sb = new StringBuilder();
            if (SensorDatas != null && SensorDatas.Count > 0)
            {
                foreach (var item in SensorDatas)
                {
                    sb.Append($"{item.Key}: {item.Value}, ");
                }
                if (sb.Length > 0)
                {
                    sb.Length -= 2; // Remove the last comma and space
                }
            }
            return sb.ToString();
        }



        public static AlarmInformation Info(string ruleName, Dictionary<string, object> datas, string description = "")
        {
            return Create(ruleName, AlarmLevel.Information, datas, description);
        }
        public static AlarmInformation Warning(string ruleName, Dictionary<string, object> datas, string description = "")
        {
            return Create(ruleName, AlarmLevel.Warning, datas,description);
        }
        public static AlarmInformation Error(string ruleName, Dictionary<string, object> datas, string description = "")
        {
            return Create(ruleName, AlarmLevel.Error, datas, description);
        }

        public static AlarmInformation Critical(string ruleName, Dictionary<string, object> datas, string description = "")
        {
            return Create(ruleName, AlarmLevel.Critical, datas, description);
        }

        public static AlarmInformation Create(string ruleName, AlarmLevel level, Dictionary<string,object> datas, string description="")
        {
            AlarmInformation alarmInformation = new AlarmInformation
            {
                AlarmId = Guid.NewGuid().ToString(),
                AlarmLevel = level,
                Description = string.IsNullOrWhiteSpace(description) ? ruleName : description,
                RuleName = ruleName,
                SensorDatas = datas,
                OccurredAt = DateTime.UtcNow
            };
            return alarmInformation;
        }
    }
}
